//
//  MapSettingViewController.m
//  location
//
//  Created by Dongxiang Xu on 2025/6/9.
//

#import "MapSettingViewController.h"

@interface MapSettingViewController ()

@property (nonatomic,strong) UIView *streetView;

@end

@implementation MapSettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = WhiteColor;
    [self initView];
}

- (void)initView
{
    UILabel *title = [[UILabel alloc] initWithFrame:CGRectMake(16, NAVHEIGHT+6, SCREEN_WIDTH-32, 32)];
    title.font = REAL_MEDIUM_FONT(24);
    title.text = @"地图设置";
    title.textColor = RealBlackColor;
    [self.view addSubview:title];
    
    NSArray *infoArray = @[@"街道",@"卫星"];
    NSArray *streetArray = @[@"显示交通状况",@"显示路网标签"];
    
    UIView *infoView = [[UIView alloc] initWithFrame:CGRectMake(16, title.maxY+16, SCREEN_WIDTH-32, 48*infoArray.count)];
    infoView.backgroundColor = RealWhiteColor;
    infoView.layer.masksToBounds = YES;
    [infoView.layer setCornerRadius:16];
    [self.view addSubview:infoView];
    
    NSInteger mapType = [[[NSUserDefaults standardUserDefaults] objectForKey:MapType] integerValue];
    for (int i = 0; i < infoArray.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, i*48, infoView.width, 48)];
        button.backgroundColor = RealWhiteColor;
        button.layer.masksToBounds = YES;
        [infoView addSubview:button];
                    
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 104, button.height)];
        label.text = infoArray[i];
        label.textColor = RealBlackColor;
        label.font = MEDIUM_FONT(15);
        [button addSubview:label];
        
        if (i==0) {
            UIImageView *line = [[UIImageView alloc] initWithFrame:CGRectMake(20, button.height-1, button.width-20, 1)];
            line.image = [UIImage imageNamed:@"line"];
            [button addSubview:line];
        }
        
        UIImageView *arrow = [[UIImageView alloc] initWithFrame:CGRectMake(button.width-36, (button.height-24)/2, 24, 24)];
        arrow.tag = 50+i;
        if (i==mapType) {
            arrow.image = [UIImage imageNamed:@"map_sel"];
        }else{
            arrow.image = nil;
        }        
        [button addSubview:arrow];
        
        [[button rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(__kindof UIControl * _Nullable x) {
            for (int i = 0; i < infoArray.count; i ++) {
                ((UIImageView *)[self.view viewWithTag:50+i]).image = nil;
            }
            arrow.image = [UIImage imageNamed:@"map_sel"];
            [[NSUserDefaults standardUserDefaults] setValue:@(i) forKey:MapType];
            [[NSUserDefaults standardUserDefaults] synchronize];
            if (i==0) {
                self.streetView.height = 48*(infoArray.count-1);
            }else{
                self.streetView.height = 48*infoArray.count;
            }
        }];
    }
    
    UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, infoView.maxY+16, SCREEN_WIDTH-32, 16)];
    label.font = REAL_MEDIUM_FONT(14);
    label.textColor = GraryHeavyLightColor;
    label.text = @"街道设置";
    [self.view addSubview:label];
    
    _streetView = [[UIView alloc] initWithFrame:CGRectMake(16, CGRectGetMaxY(label.frame)+8, SCREEN_WIDTH-32, 48*infoArray.count)];
    _streetView.backgroundColor = RealWhiteColor;
    _streetView.layer.masksToBounds = YES;
    [_streetView.layer setCornerRadius:16];
    [self.view addSubview:_streetView];
    if (mapType==0) {
        self.streetView.height = 48*(infoArray.count-1);
    }else{
        self.streetView.height = 48*infoArray.count;
    }
    
    for (int i = 0; i < infoArray.count; i ++) {
        UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(0, i*48, infoView.width, 48)];
        button.backgroundColor = RealWhiteColor;
        button.layer.masksToBounds = YES;
        [button.layer setCornerRadius:16];
        [_streetView addSubview:button];
        
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(16, 0, 104, button.height)];
        label.text = streetArray[i];
        label.textColor = RealBlackColor;
        label.font = REAL_MEDIUM_FONT(15);
        [button addSubview:label];
        
        UISwitch *aSwitch = [[UISwitch alloc] initWithFrame:CGRectMake(button.width-66, (button.height-26)/2, 48, 26)];
        [aSwitch setFrame:CGRectMake(button.width-aSwitch.width-18, (button.height-aSwitch.height)/2, aSwitch.width, aSwitch.height)];
        aSwitch.onTintColor = NormalMainColor;
        aSwitch.tintColor = RealWhiteColor;
        aSwitch.tag = 100+i;
        [aSwitch addTarget:self action:@selector(switchAction:) forControlEvents:UIControlEventValueChanged];
        if (i==0) {
            BOOL showTraffic = [[[NSUserDefaults standardUserDefaults] objectForKey:ShowTraffic] boolValue];
            [aSwitch setOn:showTraffic animated:NO];
        }else{
            BOOL roadNet = [[[NSUserDefaults standardUserDefaults] objectForKey:RoadNet] boolValue];
            [aSwitch setOn:roadNet animated:NO];
        }
       
        [button.layer setCornerRadius:12];
        [button addSubview:aSwitch];
    }
}

- (void)switchAction:(UISwitch *)sender
{
    if (sender.tag==100) {
        [[NSUserDefaults standardUserDefaults] setValue:@(sender.on) forKey:ShowTraffic];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }else if (sender.tag==101){
        [[NSUserDefaults standardUserDefaults] setValue:@(sender.on) forKey:RoadNet];
        [[NSUserDefaults standardUserDefaults] synchronize];
    }
}

@end
